package com.offcn.test;

import java.io.Serializable;

/**
 * @Author: zola
 * @Date: 2021/9/25 - 09 - 25 - 14:55
 * @Description: 单例模式:始终保证创建的对象只有一个
 * @Version: 1.0
 */
public class Student implements Serializable {
    private static final long serialVersionUID = -1685131921336147702L;

    private Integer sid;
    private String name;
    // 3.定义对象为null
    private static Student student = null; // 单例模式中的 懒汉式 模式

    // 1.构造方法私有化
    private Student(Integer sid, String name) {
        System.out.println("创建了Student对象");
        this.sid = sid;
        this.name = name;
    }
    // 2.对外提供一个暴露的接口
    public static Student getInstance(Integer sid,String name){
        if (student == null) {   // 提高效率
            synchronized (Student.class){  // synchronized (锁对象)
                if (student == null){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    student = new Student(sid, name); // 如果student为null,则穿建一个并赋值给定义号的student
                }
            }
        }
        return student; // 不为null,则返回该定义的对象
    }


    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
